/** 异步队列类 */
export class AsyncQueue<T> {
  private queue: T[] = [];
  private resolve: (() => void) | null = null;

  // 添加消息到队列
  enqueue(item: T): void {
      this.queue.push(item);
      if (this.resolve) {
          this.resolve();
          this.resolve = null;
      }
  }

  // 异步迭代器
  async *[Symbol.asyncIterator](): AsyncGenerator<T> {
      while (true) {
          if (this.queue.length === 0) {
              await new Promise<void>((resolve) => (this.resolve = resolve));
          }
          yield this.queue.shift() as T;
      }
  }
}
